package com.neusoft.cmsprojects.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.neusoft.cmsprojects.model.TbUser;
import com.neusoft.cmsprojects.util.DaoUtilImpl;

@Repository("userdao")
public class UserDaoImpl extends DaoUtilImpl implements UserDao {

	@Override
	public TbUser login(TbUser user) {
		String hql="from TbUser t where t.cusername='"+user.getCusername()+"' and t.cpassword='"+user.getCpassword()+"'";
		List<TbUser> list=(List<TbUser>)this.getHibernateTemplate().find(hql);
		if(list.size()<0){
			return null;
		}else{
		TbUser t=new TbUser();
		for (TbUser tbUser : list) {
			t=tbUser;
		}
		return t;
		}
	}

	@Override
	public void updateLoginTime(String hql) {
		SessionFactory sf=this.getHibernateTemplate().getSessionFactory();
		Session session=sf.getCurrentSession();
		session.createQuery(hql).executeUpdate();
	}

	@Override
	public boolean checkUsername(String hql) {
		int t=this.getHibernateTemplate().find(hql).size();
		if(t==0){
			return true;
		}
		return false;
	}

	@Override
	public TbUser findById(Integer cid) {
		return this.getHibernateTemplate().get(TbUser.class, cid);
	}

}
